package com.yunche.yunchebusiness.ui.view.optionView

import android.content.Context
import android.graphics.Color
import android.text.Editable
import android.text.InputType
import android.text.TextWatcher
import android.text.method.DigitsKeyListener
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.yunche.yunchebusiness.R
import com.yunche.yunchebusiness.utils.ViewHelper
import org.jetbrains.anko.find
import org.jetbrains.anko.textColor

/**
 * Create by Crcker on 2018/10/17 14:53
 * Email: Crcker@163.com
 * 自定义组合view
 * 功能：
 * 设置标题 设置内容
 * 控制显示隐藏箭头和下划线
 * 点击条目的回调，点击内容的回调
 * 内容格式的设置  内容是否可以编辑
 * 内容长度的限制
 * 内容类型：输入框 文本显示
 */

class OptionView(context: Context, attributes: AttributeSet) : LinearLayout(context, attributes), View.OnClickListener {

    private val tvTitle: TextView?
    private val ivMore: ImageView?
    private val rlBody: LinearLayout?
    private val viewLine: View?
    private val etContent: EditText?
    private var position = 0
    private var tvMust: TextView? = null
    //是否验验证成功
    var IsVerify: Boolean = true

    private var onOptionItemClickListener: OptionItemClickListener? = null

    fun setOnOptionItemClickListener(onOptionItemClickListener: OptionItemClickListener) {
        this.onOptionItemClickListener = onOptionItemClickListener
    }

    fun setTitle(title: String) {
        tvTitle?.text = title
    }

    fun setContent(content: String) {
        etContent?.setText(content)
    }

    fun getContent(): String {
        return etContent?.text.toString()
    }


    //验证类型
    var verifyType: VerifyType = VerifyType.NORMAL
        set(value) {
            field = value
            when (verifyType) {
                VerifyType.NUMBER -> etContent?.inputType = InputType.TYPE_CLASS_NUMBER
            }
        }

    //输入内容长度 默认10
    var maxLingth: Int = 100
        set(value) {
            field = value
            ViewHelper.setInputMaxLength(maxLingth, etContent)
        }

    //是否可以编辑
    var editable: Boolean = false
        set(value) {
            field = value
            etContent?.isEnabled = editable
        }

    //是否显示下划线
    var isShowLine: Boolean = true
        set(value) {
            if (viewLine != null) {
                ViewHelper.setVisibility(value, viewLine)
            }
        }
    //是否显示箭头
    var isShowArrow: Boolean = true
        set(value) {
            field = value
            ViewHelper.setVisibility(value, ivMore)
        }

    init {
        LayoutInflater.from(context).inflate(R.layout.layout_option_item, this, true)
        tvTitle = find(R.id.tv_option_title)
        //tvContent = find(R.id.tv_option_content)
        ivMore = find(R.id.iv_option_more)
        rlBody = find(R.id.rl_body)
        viewLine = find(R.id.line)
        etContent = find(R.id.et_content)
        tvMust = find(R.id.tv_option_must)
        ViewHelper.setInputMaxLength(maxLingth, etContent)
        initEvent()
        initAttr(attributes)
    }

    private fun initEvent() {
        rlBody?.setOnClickListener(this)
        etContent?.setOnClickListener(this)
        tvTitle?.setOnClickListener(this)
        etContent?.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {

            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            }
        })
    }


    private fun initAttr(attr: AttributeSet?) {
        if (attr != null) {
            var attributes = context.obtainStyledAttributes(attr, R.styleable.OptionView)
            var title = attributes.getString(R.styleable.OptionView_OptionViewTitle)
            var content = attributes.getString(R.styleable.OptionView_OptionViewContent)
            editable = attributes.getBoolean(R.styleable.OptionView_OptionViewEditable, false)
            isShowArrow = attributes.getBoolean(R.styleable.OptionView_OptionViewShowArrow, true)
            var hint = attributes.getString(R.styleable.OptionView_OptionViewHint)
            var titleTextSize = attributes.getDimension(R.styleable.OptionView_OptionViewTitleTextSize, 14f)
            var titleTextColor =
                attributes.getColor(R.styleable.OptionView_OptionViewTitleTextColor, Color.parseColor("#303437"))
            var ContentTextSize = attributes.getDimension(R.styleable.OptionView_OptionViewContentTextSize, 12f)
            var ContentTextColor =
                attributes.getColor(R.styleable.OptionView_OptionViewContentTextColor, Color.parseColor("#303437"))

            var isShowLine = attributes.getBoolean(R.styleable.OptionView_OptionViewShowLine, true)

            var isMust = attributes.getBoolean(R.styleable.OptionView_OptionViewIsMust, false)

            var digits = attributes.getString(R.styleable.OptionView_OptionViewContentDigits)
            if(digits != null){
                etContent?.keyListener = DigitsKeyListener.getInstance(digits)
            }
            position = attributes.getInt(R.styleable.OptionView_OptionViewPosition, 0)

            ViewHelper.setVisibility(isShowLine, viewLine)
            etContent?.setText(content)
            etContent?.hint = hint
            tvTitle?.text = title
            tvTitle?.textSize = titleTextSize
            etContent?.textSize = ContentTextSize
            tvTitle?.textColor = titleTextColor
            etContent?.textColor = ContentTextColor
            etContent?.isEnabled = editable
            ViewHelper.setVisibility(isMust, tvMust)
            ViewHelper.setVisibility(!editable, rlBody)
            ViewHelper.setVisibility(isShowArrow, ivMore)
            attributes.recycle()
        }
    }


    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.rl_body -> {
                if (onOptionItemClickListener != null) {
                    onOptionItemClickListener!!.onOptionItemClickListener(position)
                }
            }
            R.id.tv_content -> {
                ViewHelper.setVisibility(true, etContent!!)
            }
        }
    }

}